package com.goldcn.classify.controller;

import com.goldcn.classify.model.Style;
import com.goldcn.classify.service.*;
import com.goldcn.common.consts.RestConst;
import com.goldcn.common.exception.ServiceException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2016/5/9.
 */
@RestController
@RequestMapping(value = "/style")
public class StyleController {
    @Autowired
    private StyleService styleService;

    @Autowired
    private ClassifyStyleService classifyStyleService;

    @Autowired
    private ClassifyToAttrDefService classifyToAttrDefService;

    /**
     * 根据平台类型获取款式列表
     * @param code
     * @return
     */
    @RequestMapping(value = "/list/{code}",method = RequestMethod.GET)
    public Object loadStyleList(@PathVariable String code){
        return styleService.loadStyleList(code);
    }

    /**
     * 根据平台类型查询款式-品类列表
     * @param code
     * @return
     */
    @RequestMapping(value = "/cslist/{code}",method = RequestMethod.GET)
    public Object selectClassifyByStyle(@PathVariable String code){

        return styleService.selectClassifyByStyle(code);

    }

    /**
     * 查询款式详情 List
     * @param styleList
     * @return
     */
    @RequestMapping(value = "/InfoList", method = RequestMethod.POST)
    public Object detailList(@RequestBody List<String> styleList){
        return styleService.detailList(styleList);
    }

    /***********************/

    /**
     * 添加或修改款式 无id为调加，有id则修改
     * @param record
     * @return
     */
    @RequestMapping(method = RequestMethod.POST)
    public Object addOrUpdate(@Valid @RequestBody Style record){
        if(record.getStyleid().equals("")||record.getStyleid()==null){
            return styleService.addStyle(record);
        }else{
            return styleService.updateStyle(record);
        }
    }

    @RequestMapping(method = RequestMethod.PUT)
    public Object update(@Valid @RequestBody Style record){
        return styleService.updateStyle(record);
    }

    /**
     * 根据款式id删除款式信息
     * @param id
     * @return
     */
    @RequestMapping(value = "/{id}",method = RequestMethod.DELETE)
    public Object delete(@PathVariable String id){
        //判断是否与品类有关联
        if(!classifyStyleService.loadClassify(id).isEmpty()){
            throw new ServiceException("与品类有关联", RestConst.ErrorCode.DATABASE_ERROR);
        }
        //判断是否与属性有关联
        Map<String,String> pram=new HashMap<String,String>();
        pram.put("clazzObjID",id);
        if(!classifyToAttrDefService.selectByClazzAndAttr(pram).isEmpty()){
            throw new ServiceException("与属性有关联", RestConst.ErrorCode.DATABASE_ERROR);
        }
        return styleService.deleteStyle(id);
    }

    @RequestMapping(value = "/{id}", method = RequestMethod.GET)
    public Object detail(@PathVariable String id){
        return styleService.detail(id);
    }


}
